package com.juyu.miliao.api.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.StringUtils;
import com.juyu.miliao.api.dao.mysql.DynamicInfoDao;
import com.juyu.miliao.api.dao.redis.DynamicCommentInfoRedisDao;
import com.juyu.miliao.api.dao.redis.DynamicInfoRedisDao;
import com.juyu.miliao.api.domain.CommentReplyInfo;
import com.juyu.miliao.api.domain.DynamicCommentInfo;
import com.juyu.miliao.api.domain.DynamicInfo;
import com.juyu.miliao.api.domain.vo.DynamicDetailInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xuanqicheng
 * @create 2019-24-13:09
 */
@Component
public class DynamicInfoMq {
    private final Logger log = LoggerFactory.getLogger("dynamicRabbitMQ");

    public static final String DYNAMIC_EXCHANGE = "DYNAMIC_EXCHANGE";

    public static final String DYNAMIC_INFO_UPDATE_CACHE_ROUTING_KEY = "DYNAMIC_INFO_UPDATE_CACHE_ROUTING_KEY";

    public static final String DYNAMIC_INFO_UPDATE_CACHE_QUEUE = "DYNAMIC_INFO_UPDATE_CACHE_QUEUE";


    public static final String DYNAMIC_COMMENT_UPDATE_CACHE_ROUTING_KEY="DYNAMIC_COMMENT_CACHE_ROUTING_KEY";

    public static final String DYNAMIC_COMMENT_UPDATE_CACHE_QUEUE = "DYNAMIC_COMMENT_UPDATE_CACHE_QUEUE";

    public static final String DYNAMIC_COMMENT_REPLY_UPDATE_CACHE_ROUTING_KEY =
            "DYNAMIC_COMMENT_REPLY_UPDATE_CACHE_ROUTING_KEY";

    public static final String DYNAMIC_COMMENT_REPLY_UPDATE_CACHE_QUEUE = "DYNAMIC_COMMENT_REPLY_UPDATE_CACHE_QUEUE";



    @Autowired
    private DynamicInfoRedisDao dynamicInfoRedisDao;

    @Autowired
    private DynamicInfoDao dynamicInfoDao;

    @Autowired
    private DynamicCommentInfoRedisDao dynamicCommentInfoRedisDao;

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void getComment(String messge){
        amqpTemplate.convertAndSend(DYNAMIC_EXCHANGE,DYNAMIC_COMMENT_UPDATE_CACHE_ROUTING_KEY,messge);
    }
    /**
     * 更新动态缓存
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(DYNAMIC_INFO_UPDATE_CACHE_QUEUE),
            exchange = @Exchange(DYNAMIC_EXCHANGE),
            key = {DYNAMIC_INFO_UPDATE_CACHE_ROUTING_KEY}))
    public void getUpdateDynamicCache(String message) {
        try {
            if (!StringUtils.isEmpty(message)) {

                long dynamicId = Long.parseLong(message);

                 DynamicDetailInfoVo obj =dynamicInfoDao.getDynamicInfoNotStatusById(dynamicId);
                if(obj !=null){
                    DynamicInfo dynamicInfo = new DynamicInfo();

                    BeanUtils.copyProperties(obj,dynamicInfo);

                    dynamicInfoRedisDao.deleteListCache(dynamicInfo);
                    log.info("更新动态信息："+message);
                }

            }

        } catch (Exception ex) {
            log.error("[更新动态缓存]错误:" + ex.getMessage());
        }
    }


    /**
     * 更新动态评论缓存缓存
     * @param message, 动态评论信息，json字符串
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(DYNAMIC_COMMENT_UPDATE_CACHE_QUEUE),
            exchange = @Exchange(DYNAMIC_EXCHANGE),
            key = {DYNAMIC_COMMENT_UPDATE_CACHE_ROUTING_KEY}))
    public void getUpdateCommentCache(String message) {
        try {
            log.info("动态评论信息JSON字符串："+message);
            if (!StringUtils.isEmpty(message)) {

                DynamicCommentInfo dynamicCommentInfo = JSONObject.parseObject(message,DynamicCommentInfo.class);
                log.info("动态评论信息："+JSONObject.toJSONString(dynamicCommentInfo));
                dynamicCommentInfoRedisDao.deleteDynamicCommentInfo(dynamicCommentInfo);

                log.info("更新动态评论缓存缓存："+JSONObject.toJSONString(dynamicCommentInfo));
            }

        } catch (Exception ex) {
            log.error("[更新动态评论缓存缓存]错误:" + ex.getMessage());
        }
    }


    /**
     * 更新动态评论回复信息缓存
     * @param message, 动态评论回复信息，json字符串
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(DYNAMIC_COMMENT_REPLY_UPDATE_CACHE_QUEUE),
            exchange = @Exchange(DYNAMIC_EXCHANGE),
            key = {DYNAMIC_COMMENT_REPLY_UPDATE_CACHE_ROUTING_KEY}))
    public void getUpdateCommentReplyCache(String message) {
        try {
            if (!StringUtils.isEmpty(message)) {

                CommentReplyInfo commentReplyInfo = JSONObject.parseObject(message,CommentReplyInfo.class);

                dynamicCommentInfoRedisDao.deleteCommentReplyInfo(commentReplyInfo);

                log.info("更新动态评论回复信息缓存："+JSONObject.toJSONString(commentReplyInfo));
            }

        } catch (Exception ex) {
            log.error("[更新动态评论回复缓存缓存]错误:" + ex.getMessage());
        }
    }
}
